package com.xueyu.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;

import com.xueyu.dao.BookTypeDao;
import com.xueyu.model.BookType;
import com.xueyu.util.DbUtil;
import com.xueyu.util.StringUtil;

public class BookTypeAddInternalFrame extends JInternalFrame {
	private JTextField bookTypeNameTxt;
	private JTextArea bookTypeDescTxt;
	private DbUtil dbUtil = new DbUtil();
	private BookTypeDao bookTypeDao = new BookTypeDao();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookTypeAddInternalFrame frame = new BookTypeAddInternalFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookTypeAddInternalFrame() {
		setTitle("\u56FE\u4E66\u7C7B\u522B\u7BA1\u7406");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 450, 300);

		JLabel label = new JLabel("\u56FE\u4E66\u7C7B\u522B\u540D\u79F0");

		JLabel label_1 = new JLabel("\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0");

		bookTypeNameTxt = new JTextField();
		bookTypeNameTxt.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		JButton button = new JButton("\u6DFB\u52A0");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeAddActionPerformed();
			}
		});
		button.setIcon(new ImageIcon(BookTypeAddInternalFrame.class.getResource("/image/add.png")));

		JButton btnNewButton = new JButton("\u91CD\u7F6E");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetActionPerformed();
			}
		});
		btnNewButton.setIcon(new ImageIcon(BookTypeAddInternalFrame.class.getResource("/image/reset.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(Alignment.LEADING,
				groupLayout.createSequentialGroup().addGap(80)
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addComponent(button)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(label)
										.addComponent(label_1)))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
								.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 224,
												Short.MAX_VALUE)
										.addComponent(bookTypeNameTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 224,
												Short.MAX_VALUE))
								.addContainerGap(33, GroupLayout.PREFERRED_SIZE)).addGroup(Alignment.TRAILING,
										groupLayout.createSequentialGroup().addComponent(btnNewButton).addGap(91)))));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(49)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(
								bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
				.addGap(27)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(label_1)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
				.addGap(22).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(button)
						.addComponent(btnNewButton)).addGap(23)));

		bookTypeDescTxt = new JTextArea();
		scrollPane.setViewportView(bookTypeDescTxt);
		getContentPane().setLayout(groupLayout);
		// 设置文本域边框
		bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 1, false));
	}
	/**
	 * 图书类别添加事件处理
	 */
	private void bookTypeAddActionPerformed() {
		String bookTypeName = this.bookTypeNameTxt.getText();
		String bookTypeDesc = this.bookTypeDescTxt.getText();
		if(StringUtil.isEmpty(bookTypeName)){
			JOptionPane.showMessageDialog(null, "图书类别名不能为空！");
			return;
		}
		Connection con = null;
		BookType bookType = new BookType(bookTypeName,bookTypeDesc);
		try {
			con = dbUtil.getCon();
			int ret = bookTypeDao.add(con, bookType);
			if(ret==1){
				JOptionPane.showMessageDialog(null, "添加成功！");
				resetValue();
			}else{
				JOptionPane.showMessageDialog(null, "添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "添加失败");
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "something wrong!");
			}
		}
		
	}

	/**
	 * 重置事件处理
	 */
	private void resetActionPerformed() {
		resetValue();
	}

	/**
	 * 表单重置
	 */
	private void resetValue() {
		this.bookTypeNameTxt.setText("");
		this.bookTypeDescTxt.setText("");
	}
}
